package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Vector;

import view.AdminContactView;
import view.HomeCalendar;
import view.HomeEvent;

public class SearchEvent extends Observable {

	public static SearchEvent evn = null;
	public Vector<Evento> search = new Vector<Evento>();
	public static Vector<Evento> allPublic = new Vector<Evento>();
	public static Vector<Evento> allPrivate = new Vector<Evento>();
	public static Vector<Notifica> notify;

	public SearchEvent() {
		evn = this;
	}

	public static Vector<Evento> getAdminVectEventOfDate(String gg, String mm,String aa) {
		Vector<Evento> ev = new Vector<Evento>();
		//Vector<Integer> id_inv = new Vector<Integer>();
		try {
			for (int i=0;i<AdminContactView.selectContact.size();i++){
				ResultSet result = Database.getQueryResult("SELECT * FROM evento WHERE day LIKE '"+ gg + "/" + mm + "/" + aa + "' AND acc_id = "+ AdminContactView.selectContact.get(i));
				while (result.next()){
					boolean flag = true;
					for (int x=0; x< ev.size();x++)
						if (ev.get(x).getEvn_id() == result.getInt("evn_id")){
							flag = false;
							break;
						}
					if (flag)
						ev.add(new Evento(result.getInt("evn_id"), result
								.getString("nome"), result.getString("descrizione"),
								result.getString("luogo"), result.getString("day"),
								result.getString("time"), result.getString("type"),
								result.getInt("level"), result.getInt("acc_id")));
				}
				result = Database.getQueryResult("SELECT evento.* FROM (evento JOIN invitati ON evento.evn_id=invitati.evn_id) WHERE inv_id="
						+ AdminContactView.selectContact.get(i)+ " AND day LIKE '"+ gg+ "/" + mm + "/" + aa + "'");
				while (result.next()){
					boolean flag = true;
					for (int x=0; x< ev.size();x++)
						if (ev.get(x).getEvn_id() == result.getInt("evn_id")){
							flag = false;
							break;
						}
					if (flag)
						ev.add(new Evento(result.getInt("evn_id"), result
								.getString("nome"), result.getString("descrizione"),
								result.getString("luogo"), result.getString("day"),
								result.getString("time"), result.getString("type"),
								result.getInt("level"), result.getInt("acc_id")));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ev;
	}

	public static int getAdminLevelEventOfDate(String gg, String mm, String aa) {
		int type = 0;
		int t = -1;
		try {
			for (int i=0;i<AdminContactView.selectContact.size();i++){
				ResultSet result = Database.getQueryResult("SELECT level,acc_id FROM evento WHERE day LIKE '"+ gg + "/" + mm + "/" + aa + "'");
				while (result.next()) {
					type = result.getInt("level");
					if (type == 1
							&& AdminContactView.selectContact.get(i) == result.getInt("acc_id"))
						t = 1;
					else if (type != 1)
						t = 0;

					//	System.out.println("type=" + type + " t=" + t);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;

	}

	public static Vector<Evento> getVectEventOfDate(String gg, String mm,String aa) {
		Vector<Evento> ev = new Vector<Evento>();
		//Vector<Integer> id_inv = new Vector<Integer>();
		try {
			ResultSet result = Database.getQueryResult("SELECT * FROM evento WHERE level = 0 AND day LIKE '"+ gg+ "/"+ mm+ "/"+ aa	+ "' AND NOT acc_id = "	+ Database.Utente.getID());
			while (result.next())
				ev.add(new Evento(result.getInt("evn_id"), result
						.getString("nome"), result.getString("descrizione"),
						result.getString("luogo"), result.getString("day"),
						result.getString("time"), result.getString("type"), 0,
						result.getInt("acc_id")));
			result = Database.getQueryResult("SELECT * FROM evento WHERE day LIKE '"+ gg + "/" + mm + "/" + aa + "' AND acc_id = "+ Database.Utente.getID());
			while (result.next())
				ev.add(new Evento(result.getInt("evn_id"), result
						.getString("nome"), result.getString("descrizione"),
						result.getString("luogo"), result.getString("day"),
						result.getString("time"), result.getString("type"),
						result.getInt("level"), result.getInt("acc_id")));
			result = Database.getQueryResult("SELECT evento.* FROM (evento JOIN invitati ON evento.evn_id=invitati.evn_id) WHERE inv_id="
					+ Database.Utente.getID()+ " AND day LIKE '"+ gg+ "/" + mm + "/" + aa + "'");
			while (result.next())
				ev.add(new Evento(result.getInt("evn_id"), result
						.getString("nome"), result.getString("descrizione"),
						result.getString("luogo"), result.getString("day"),
						result.getString("time"), result.getString("type"),
						result.getInt("level"), result.getInt("acc_id")));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ev;
	}

	public Vector<Evento> getAllPublicEvents() {
		try {
			allPublic.clear();
			ResultSet result = Database.getQueryResult("SELECT * FROM evento WHERE level = 0 AND NOT acc_id = "
					+ Database.Utente.getID());
			while (result.next())
				allPublic.add(new Evento(result.getInt("evn_id"), result
						.getString("nome"), result.getString("descrizione"),
						result.getString("luogo"), result.getString("day"),
						result.getString("time"), result.getString("type"), 0,
						result.getInt("acc_id")));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return allPublic;
	}

	public Vector<Evento> getAllPrivateEventsOfAccount() {
		try {
			allPrivate.clear();
			ResultSet result = Database.getQueryResult("SELECT * FROM evento WHERE acc_id = "
					+ Database.Utente.getID());
			while (result.next())
				allPrivate.add(new Evento(result.getInt("evn_id"), result
						.getString("nome"), result.getString("descrizione"),
						result.getString("luogo"), result.getString("day"),
						result.getString("time"), result.getString("type"),
						result.getInt("level"), result.getInt("acc_id")));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return allPrivate;
	}

	public static Evento getEventFromId(int id) {
		Evento event = null;
		try {
			ResultSet result = Database.getQueryResult("SELECT * FROM evento WHERE evn_id=" + id);
			while (result.next())
				event = new Evento(id, result.getString("nome"),
						result.getString("descrizione"),
						result.getString("luogo"), result.getString("day"),
						result.getString("time"), result.getString("type"),
						result.getInt("level"), result.getInt("acc_id"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return event;
	}

	public static Vector<Integer> getInvitatiOfEvent(int evn_id) {
		Vector<Integer> inv = new Vector<Integer>();
		try {
			ResultSet result = Database.getQueryResult("SELECT * FROM invitati WHERE evn_id="+ evn_id);
			while (result.next())
				inv.add(result.getInt("inv_id"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return inv;
	}

	public void uppdater() {
		setChanged();
		notifyObservers(HomeEvent.class);
		notifyObservers(HomeCalendar.class);
	}

	public static int getLevelEventOfDate(String gg, String mm, String aa) {
		int type = 0;
		int t = -1;
		try {
			ResultSet result = Database.getQueryResult("SELECT level,acc_id FROM evento WHERE day LIKE '"+ gg + "/" + mm + "/" + aa + "'");
			while (result.next()) {
				type = result.getInt("level");
				if (type == 1
						&& Database.Utente.getID() == result.getInt("acc_id"))
					t = 1;
				else if (type != 1)
					t = 0;

				//	System.out.println("type=" + type + " t=" + t);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}

	public static int getLevelEventById(int evn_id) {
		int type = 0;
		try {
			ResultSet result = Database.getQueryResult("SELECT level FROM evento WHERE evn_id  = "	+ evn_id);
			while (result.next())
				type = result.getInt("level");
			// System.out.println("type = " +type);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return type;
	}

	public static Vector<Notifica> getNotifyOfAccount(int acc_id) {
		try {
			notify = new Vector<Notifica>();
			ResultSet result = Database.getQueryResult("SELECT * FROM notifica WHERE acc_id  = "+ acc_id);
			while (result.next())
				notify.add(new Notifica(result.getInt("not_id"), result
						.getString("corpo"), result.getInt("flag"), result
						.getInt("tipo"), acc_id));
			// System.out.println("type = " +type);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return notify;
	}
}
